package com.webmagic.demo.Instrument;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * @description
 * @Auther zhanglu
 * @Date 2018/4/17 上午1:28
 */
@Slf4j
@Service
public class MvProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setCharset("UTF-8").setSleepTime(100);

    @Autowired
    private FileConfig fileConfig;

    @Value("${file.start}")
    @Getter
    public int num;

    @Override
    public void process(Page page) {
        try {
            if (num > fileConfig.getEnd()) {
                log.warn("大于 {}", fileConfig.getEnd());
                return;
            }
            String product = "//div[@class=\"solid_gray\"]";
            page.getHtml().xpath(product).get();//先解析一波，后面能解析出来可能性高，我也不知道为什么。fuck!
            Instrument instrument = new Instrument();
            instrument.setSupplier(page.getHtml().xpath("//div[@class=\"pro_R\"]/h3[2]/a/text()").get());
            instrument.setSupplierUrl(page.getHtml().xpath("//div[@class=\"pro_R\"]/h3[2]/a/@href").get());
            instrument.setCategoryOne(page.getHtml().xpath("//div[@class=\"fontArial\"]/a[3]/text()").get());
            instrument.setCategoryTwo(page.getHtml().xpath("//div[@class=\"fontArial\"]/a[4]/text()").get());
            instrument.setCategoryThree(page.getHtml().xpath("//div[@class=\"fontArial\"]/a[5]/text()").get());
            instrument.setChineseName(page.getHtml().xpath("//div[@class=\"fontArial\"]/a[6]/text()").get());
            instrument.setUrl(page.getHtml().xpath("//div[@class=\"fontArial\"]/a[6]/@href").get());
            instrument.setBrand(page.getHtml().xpath("//div[@class=\"info_con\"]/table/tbody/tr[1]/td[2]/span/a/text()").get());
            instrument.setModel(page.getHtml().xpath("//div[@class=\"info_con\"]/table/tbody/tr[1]/td[1]/span/text()").get());
            instrument.setQuote(page.getHtml().xpath("//div[@class=\"info_con\"]/table/tbody/tr[2]/td[2]/span/text()").get());
            instrument.setOriginPlace(page.getHtml().xpath("//div[@class=\"info_con\"]/table/tbody/tr[3]/td[2]/span/text()").get());
            instrument.setIntroduction(page.getHtml().xpath("//div[@class=\"IMShowApplyClass\"]/html()").get());
            page.putField("bean", instrument);

            num++;
            String targetUrl = "http://www.instrument.com.cn/netshow/C" + num + ".htm";
            page.addTargetRequest(targetUrl);
        } catch (Exception e) {
            log.error("", e);
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}
